(function (_super) {
    var io = ppt.io;

    var EXTRA_ANI = {
        "win": "riband",
        "lose": "rain",
        "draw": "riband"
    }

    function Result(data) {
        Result.super(this);

        this.ASSETS = {
            RES: ASSETS.RESULT,
            TYPE: "simple"
        };

        this.BG = null;
        this.ani = null;
        this.resultUI = null;
        this.operateUI = null;
        this.resultAni = null;
        this.resultState = null;
        this.isUpgrade = null;
        this.data = data;

        this.init(data);
    }
    Laya.class(Result, "Scene.Result", _super);
    var _proto = Result.prototype;

    _proto.init = function (data) {
        this.size(GAME_CONFIG.WIDTH, Laya.stage.height);

        switch (data.mode) {
            case "pve":
                this.resultState = data.res;
                this.isUpgrade = data.isUpgrade;
                break;
            case "pvp":
                var roles = data.roles;
                for (var i in roles) {
                    if (roles[i].userId == userId) {
                        this.resultState = roles[i].res;
                        this.isUpgrade = roles[i].isUpgrade;
                        break;
                    }
                }
                break;
        }

        io.register("game::ready", this, this.gotoGame);
        io.register("pvp::wait", this, this.gotoWaiting);
        io.register("pve::wait", this, this.showPveMsg);
    }
    _proto.gotoGame = function (data) {
        console.log("Result Scene to GameScene");
        ppt.director.runScene(new Scene.Game(data));
    }
    _proto.gotoWaiting = function (data) {
        if (data.code == 107) {
           
            var config = {
                msg: data.msg,
                close: true,
                btn: {
                    btnCancel: {
                        skin: "cancel"
                    },
                    btnConfirm: {
                        skin: "confirm"
                    }
                }
            }
            ppt.director.popScene(new Alert.Public(config));
            return;
        }
        ppt.director.runScene(new Scene.Waiting(data));
    }
    _proto.showPveMsg = function (data) {
        if (data.code == 103) {
            var config = {
                msg: data.msg,
                close: true,
                btn: {
                    btnCancel: {
                        skin: "cancel"
                    },
                    btnConfirm: {
                        skin: "confirm"
                    }
                }
            }
            ppt.director.popScene(new Alert.Public(config));
        }
    }


    _proto.onAdd = function () {
        var data = this.data;
        var stageHeight = Laya.stage.height;
        var resultUI = null;

        // var filter = new Laya.BlurFilter();
        //     filter.strength = 5;

        var resultAni = utils.createSkeleton("res/result/" + EXTRA_ANI[this.resultState]);
            resultAni.pos(GAME_CONFIG.WIDTH / 2, stageHeight / 2);

        var ani = utils.createSkeleton("res/result/result_ani");
            ani.pos(GAME_CONFIG.WIDTH / 2, stageHeight / 2 - 205);
            ani.play(this.resultState + "_enter", false);
            ani.once(Laya.Event.STOPPED, this, function () {
                this.ani.play(this.resultState + "_loop", true);
            });

        var BG = utils.createSkeleton("res/public/public_bg");
            BG.pos(GAME_CONFIG.WIDTH / 2, stageHeight / 2);
            BG.play(0, true);
            // BG.filters = [filter];

        var operateUI = new Com.Result.Operate(data);

        switch (data.mode) {
            case "pve":
                resultUI = new Com.Result.PVE(data);
                break;
            case "pvp":
                resultUI = new Com.Result.PVP(data);
                break;
        }

        this.BG = BG;
        this.ani = ani;
        this.resultUI = resultUI;
        this.operateUI = operateUI;
        this.resultAni = resultAni;
        this.addChildren(BG, ani, resultUI, operateUI, resultAni);
    }
    _proto.onEnter = function () {
        this.resultAni.play(0, false);

        this.resultUI.enter();
        this.operateUI.enter();

        if (this.isUpgrade) {
            ppt.director.popScene(new Alert.Upgrade);
        };
    }
    _proto.onExit = function () {
        this.destroy(true);

        io.unregister("game::ready", this.gotoGame);
        io.unregister("pvp::wait", this.gotoWaiting);
        io.unregister("pve::wait", this.showPveMsg);

        this.BG = null;
        this.ani = null;
        this.resultUI = null;
        this.operateUI = null;
        this.resultAni = null;
        this.resultState = null;
        this.isUpgrade = null;
        this.data = null;
    }
    _proto.onResize = function (height) {
        this.height = height;

        this.BG.y = height / 2;
        this.ani.y = height / 2 - 205;
        this.resultUI.resize(height);
        this.operateUI.resize(height);
    }
})(Laya.Sprite);